﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using LearnNet.Common;
using System.Drawing;
using System.Reflection;
using Advertise;
namespace Advertise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       
        protected RegistEntity regEntity = new RegistEntity();
        private XmlHelper xmlHelper = new XmlHelper(string.Concat(Application.StartupPath, "/config.xml"));
        private void Form1_Load(object sender, EventArgs e)
        {
         
            XmlNodeList nodeList = xmlHelper.GetList("root/station");
            foreach (XmlNode node in nodeList)
            {
                comboBox1.Items.Add(node.Attributes["code"].Value);
            }
 
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
            webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
        }
         
       
        string str = "";
        public string username(int x)
        {
            char[] c = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            Random ran = new Random();
            string sd = "";
            for (int i = 0; i <= x-1; i++)//5个char类型的字符赋值给一个string类形的字符串sd,sd的任务是每次刷新，str的作用是全局变量
            {
                sd = string.Concat(sd, c[ran.Next(c.Length)]);
            }
            str =string.Concat("y", sd);
            return str.ToLower();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请填写验证码");
                return;
            }
            if (comboBox1.SelectedIndex < 0)
            {
                MessageBox.Show("请选择注册类型");
                return;
            }
         tbName.Text=   regEntity.Name = "yyy" + username(10);
         tbPwd.Text=   regEntity.Pwd = "qwe123";
            regEntity.SurePwd = "qwe123";
            regEntity.QQ = "601588935";
            regEntity.Question = "3";
            regEntity.Answer = "我是王相同";
            regEntity.Year = "1987";
            regEntity.Month = "10";
            regEntity.Day = "20";
            regEntity.Email = tbEmail.Text;
            regEntity.VerificationCode = textBox1.Text;
            regEntity.Nick = "lbxyzef";
            object[] parameters = new object[2];
            parameters[0] = webBrowser1;
            parameters[1] = xmlHelper;
            try
            {
                string typeName = "Advertise.Repost" + comboBox1.Items[comboBox1.SelectedIndex].ToString();
                Assembly asm = Assembly.GetExecutingAssembly();

                Object obj = asm.CreateInstance(typeName, true, BindingFlags.Default, null, parameters, null, null);

                IRepost irepost = obj as IRepost;
                irepost.Register(regEntity);
                btnShow.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                btnShow.Enabled = false;
            }
            
        }

       


        //中国最大的８０后编程社区欢迎您http://bbs.ccsdn.com
        private void timer1_Tick(object sender, EventArgs e)
        {
            //if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            //{
            //    Point p = new Point(100, 660);
            //    webBrowser1.Document.Window.ScrollTo(p);
            //}
            //else
            //{
            //   // MessageBox.Show(webBrowser1.ReadyState.ToString());

            //}
        }
        

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                XmlNode xmlNode = xmlHelper.GetNode("root/station[@code='" + comboBox1.Items[comboBox1.SelectedIndex].ToString() + "']/register");
                if (xmlNode.Attributes["url"] != null)
                {
                    this.webBrowser1.Url = new Uri(xmlNode.Attributes["url"].Value);
                }
                btnRReg.Enabled = false;
                btnReg.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                btnRReg.Enabled = true;
                btnReg.Enabled = false;
            }
          
        }

        protected void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {

           // MessageBox.Show(e.Url.AbsoluteUri);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Url = new Uri("http://control.blog.sina.com.cn/admin/article/article_add.php?index");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           XmlNode xmlNode= xmlHelper.GetNode("root/station[@code='" + comboBox1.Items[comboBox1.SelectedIndex].ToString() + "']/register");
           if (xmlNode.Attributes["url"] != null)
           {
               this.webBrowser1.Url = new Uri(xmlNode.Attributes["url"].Value);
           }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if(tbName.Text==""){
                MessageBox.Show("请填写名称");
                return;
            }
            if(tbPwd.Text==""){
                MessageBox.Show("请填写密码");
                return;
            }
            int count = 0;
            try
            {
                string str = string.Empty;
                str = "select count(*) from UserInfo where name='" + tbName.Text + "' and website='" + comboBox1.Items[comboBox1.SelectedIndex].ToString() + "'";
                count = Utils.StringToInt(LearnNet.DAL.DALHelper.LearnNetHelper.ExecuteScalar(CommandType.Text, str, null), 0);

                if (count > 0)
                {
                    MessageBox.Show("已经添加过该数据");
                    return;

                }
                str = string.Concat("insert into UserInfo(name,pwd,website) values('", tbName.Text, "','", tbPwd.Text, "','", comboBox1.Items[comboBox1.SelectedIndex].ToString(), "')");

                LearnNet.DAL.DALHelper.LearnNetHelper.ExecuteNonQuery(CommandType.Text, str, null);
                btnRReg.Enabled = true;
                btnShow.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                btnRReg.Enabled = false;
            }
           
        }


    }
}
